import Vue from 'vue'

//按数据类型初始化
const initField = (obj, models) => {
    for (let key in models) {
        Vue.set(obj, key, models[key].type())
    }
}

//设置属性值
const setField = (obj, models, value) => {
    if (typeOf(value) == "object") {
        //根据models中配置的field属性获取属性值
        for (let key in models) {
            let k = models[key].field ? models[key].field : key
            obj[key] = value[k]
        }
    } else if (typeOf(value) == "array" && Object.keys(models).length > 1) {
        //根据models的顺序与组织中值的顺序一致
        let keys = Object.keys(models)
        for (let index in value) {
            obj[keys[index]] = value[index]
        }
    } else {
        for (let _key in Object.keys(models)) {
            obj[Object.keys(models)[_key]] = value
        }
    }
}

//判断数据类型
const typeOf = (obj) => {
    const toString = Object.prototype.toString;
    const map = {
        '[object Boolean]': 'boolean',
        '[object Number]': 'number',
        '[object String]': 'string',
        '[object Function]': 'function',
        '[object Array]': 'array',
        '[object Date]': 'date',
        '[object RegExp]': 'regExp',
        '[object Undefined]': 'undefined',
        '[object Null]': 'null',
        '[object Object]': 'object'
    };
    return map[toString.call(obj)];
}

//防抖
const debounce = (fn, delay) => {
    let timer = null
    return function () {
        let arg = arguments

        clearTimeout(timer)

        timer = setTimeout(() => {
            fn.apply(this, arg)
        }, delay)
    }
}

const transformTozTreeFormat = (setting, sNodes) => {
    var i, l,
        { key, parentKey, childKey } = setting;

    if (!key || key == "" || !sNodes) return [];

    if (Array.isArray(sNodes)) {
        var r = [];
        var tmpMap = [];
        for (i = 0, l = sNodes.length; i < l; i++) {
            tmpMap[sNodes[i][key]] = sNodes[i];
        }
        for (i = 0, l = sNodes.length; i < l; i++) {
            if (tmpMap[sNodes[i][parentKey]] && sNodes[i][key] != sNodes[i][parentKey]) {
                if (!tmpMap[sNodes[i][parentKey]][childKey])
                    tmpMap[sNodes[i][parentKey]][childKey] = [];
                tmpMap[sNodes[i][parentKey]][childKey].push(sNodes[i]);
            } else {
                r.push(sNodes[i]);
            }
        }
        return r;
    } else {
        return [sNodes];
    }
}
const transformToArrayFormat = (setting, nodes) => {
    if (!nodes) return [];
    // var childKey = setting.data.key.children,
    var childKey = "children",
        r = [];
    if (Array.isArray(nodes)) {
        for (var i = 0, l = nodes.length; i < l; i++) {
            r.push(nodes[i]);
            if (nodes[i][childKey])
                r = r.concat(transformToArrayFormat(setting, nodes[i][childKey]));
        }
    } else {
        r.push(nodes);
        if (nodes[childKey])
            r = r.concat(transformToArrayFormat(setting, nodes[childKey]));
    }
    return r;
}

//根据出生日期计算年龄
const age = (value) => {
    let returnAge
    let strBirthdayArr = new Date(value).toLocaleDateString().split("/")
    let birthYear = strBirthdayArr[0]
    let birthMonth = strBirthdayArr[1]
    let birthDay = strBirthdayArr[2]

    let d = new Date()
    let nowYear = d.getFullYear()
    let nowMonth = d.getMonth() + 1
    let nowDay = d.getDate()

    if (nowYear == birthYear) {
        returnAge = 0;//同年 则为0岁
    }
    else {
        let ageDiff = nowYear - birthYear; //年之差
        if (ageDiff > 0) {
            if (nowMonth == birthMonth) {
                let dayDiff = nowDay - birthDay;//日之差
                if (dayDiff < 0) {
                    returnAge = ageDiff - 1;
                }
                else {
                    returnAge = ageDiff;
                }
            }
            else {
                let monthDiff = nowMonth - birthMonth;//月之差
                if (monthDiff < 0) {
                    returnAge = ageDiff - 1;
                }
                else {
                    returnAge = ageDiff;
                }
            }
        }
        else {
            returnAge = -1;//返回-1 表示出生日期输入错误 晚于今天
        }
    }

    return returnAge;//返回周岁年龄
}
const isValidRightRQ = (idCard) => {
    //按照身份证获取性别
    if (idCard.length == 18) {
        if (idCard.substring(14, 17) % 2 == 0) {
            return '女';
        } else {
            return '男';
        }
    }
    if (idCard.length == 15) {
        if (idCard.substring(14, 15) % 2 == 0) {
            return '女';
        } else {
            return '男';
        }
    }
}
const GetBirthdayByIdCardNo = (idCard) => {
    //按照身份证号获取出生日期
    if (idCard.length == 18) {
        return GetBirthdayByIdCardNo18(idCard);
    }
    if (idCard.length == 15) {
        return GetBirthdayByIdCardNo15(idCard);
    }
}
const GetBirthdayByIdCardNo18 = (idCard18) => {
    var year = idCard18.substring(6, 10);
    var month = idCard18.substring(10, 12);
    var day = idCard18.substring(12, 14);
    return year + '-' + month + '-' + day;
}
const GetBirthdayByIdCardNo15 = (idCard15) => {
    var year = idCard15.substring(6, 8);
    var month = idCard15.substring(8, 10);
    var day = idCard15.substring(10, 12);
    return "19" + year + '-' + month + '-' + day;
}
export { 
    initField, 
    setField, 
    typeOf, 
    debounce, 
    transformTozTreeFormat, 
    transformToArrayFormat, 
    age, 
    isValidRightRQ,
    GetBirthdayByIdCardNo,
    GetBirthdayByIdCardNo18,
    GetBirthdayByIdCardNo15
}